/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import com.mysql.jdbc.CallableStatement;
import dbhelper.dbconnect;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import POJO.sanPhamCuaHangPOJO;
/**
 *
 * @author SoftVnn Member
 */
public class sanPhamCuaHangDAO {
    public ArrayList<sanPhamCuaHangPOJO> getAll()
    {
        ArrayList<sanPhamCuaHangPOJO> arr = new ArrayList<sanPhamCuaHangPOJO>();
        try
        {
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spSanPham_CuaHang_getAll();");
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            st.execute();
            ResultSet rs = st.getResultSet();
            while (rs.next())
            {
                sanPhamCuaHangPOJO obj = new sanPhamCuaHangPOJO();
                obj.setMaCuahang(rs.getString("MaCuaHang"));
                obj.setMaSanPham(rs.getString("MSanPham"));
                obj.setSoLuongCon(rs.getInt("SoLuongCon"));
                arr.add(obj);
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error;
        }
        return arr;
    }
    public ArrayList<sanPhamCuaHangPOJO> get(String MaCuaHang, String MaSanPham)
    {
        ArrayList<sanPhamCuaHangPOJO> arr = new ArrayList<sanPhamCuaHangPOJO>();
        try
        {           
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spSanPham_CuaHang_get('%s','%s');", MaCuaHang,MaSanPham);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            st.execute();
            ResultSet rs = st.getResultSet();
            while (rs.next())
            {
                sanPhamCuaHangPOJO obj = new sanPhamCuaHangPOJO();
                obj.setMaCuahang(rs.getString("MaCuaHang"));
                obj.setMaSanPham(rs.getString("MSanPham"));
                obj.setSoLuongCon(rs.getInt("SoLuongCon"));
                arr.add(obj);
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return arr;
    }
    public int insert(String MaCuaHang, String MaSanPham, int SoLuongCon)
    {
        int result = 0;
        try
        {           
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spSanPham_CuaHang_insert('%s','%s','%d');", MaCuaHang,MaSanPham,SoLuongCon);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            if(st.execute()== false)
            {
                result = 1;
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return result;
    }
    public int update(String MaCuaHang, String MaSanPham, int SoLuongCon)
    {
        int result = 0;
        try{
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spSanPham_CuaHang_update('%s','%s','%d');", MaCuaHang, MaSanPham,SoLuongCon);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            if(st.execute()== false)
            {
                result = 1;
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return result;
    }
    public int delete(String MaCuaHang, String MaSanPham)
    {
        int result = 0;
        try{
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spSanPham_CuaHang_delete('%s','%s');", MaCuaHang,MaSanPham);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            if(st.execute()== false)
            {
                result = 1;
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return result;
    }
    public ArrayList<sanPhamCuaHangPOJO> getMaCuaHang(String MaCuaHang)
    {
        ArrayList<sanPhamCuaHangPOJO> arr = new ArrayList<sanPhamCuaHangPOJO>();
        try
        {           
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spSanPham_CuaHang_getMaCuaHang('%s');", MaCuaHang);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            st.execute();
            ResultSet rs = st.getResultSet();
            while (rs.next())
            {
                sanPhamCuaHangPOJO obj = new sanPhamCuaHangPOJO();
                obj.setMaCuahang(rs.getString("MaCuaHang"));
                obj.setMaSanPham(rs.getString("MSanPham"));
                obj.setSoLuongCon(rs.getInt("SoLuongCon"));
                arr.add(obj);
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return arr;
    }
    public ArrayList<sanPhamCuaHangPOJO> getMaSanPham(String MaSanPham)
    {
        ArrayList<sanPhamCuaHangPOJO> arr = new ArrayList<sanPhamCuaHangPOJO>();
        try
        {           
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spSanPham_CuaHang_getMaSanPham('%s');", MaSanPham);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            st.execute();
            ResultSet rs = st.getResultSet();
            while (rs.next())
            {
                sanPhamCuaHangPOJO obj = new sanPhamCuaHangPOJO();
                obj.setMaCuahang(rs.getString("MaCuaHang"));
                obj.setMaSanPham(rs.getString("MSanPham"));
                obj.setSoLuongCon(rs.getInt("SoLuongCon"));
                arr.add(obj);
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return arr;
    }
}
